<template>
  <div class="my_network_box">
    <div class="add_btn_top">
      <el-button
        v-permission="[24, 'button']"
        @click="handAdd"
        type="primary"
        icon="el-icon-plus"
        size="mini"
        >添加</el-button
      >
    </div>
    <div v-permission="[27, 'list']">
      <div v-init-screen-h>
        <el-row :gutter="20" v-if="tableData && tableData.length !== 0">
          <el-col
            style="margin-bottom: 20px"
            :xs="12"
            :sm="12"
            :md="8"
            :lg="6"
            :xl="4"
            v-for="item in tableData"
            :key="item.id"
          >
            <div @click="handNavTo(item)" class="item">
              <img class="img" :src="item.imageUrl | filtersFileUrl()" alt="" />
              <div class="content">
                <div class="title">{{ item.name }}</div>
                <div class="summary">
                  {{ item.description }}
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      <empty-data
        v-if="tableData && tableData.length == 0"
        v-init-screen-h
      ></empty-data>
      <Pagination
        @pagination="pagination"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        :pageSizes.sync="pageSizes"
      />
    </div>

    <AddDialog
      ref="addDialgRef"
      :typeList="typeList"
      :showDialog="addFlag"
      @closeDialog="closeDialog"
      :editmetaData="editmetaData"
      :isedit="isedit"
      :token="token"
    ></AddDialog>
  </div>
</template>

<script>
import Pagination from "@/views/common/page";
import { getAssetsList, getAssettypes, getAssetsDetails } from "@/api/assets";
import AddDialog from "./components/AddDialog.vue";
import emptyData from "@/views/common/emptyData";
import _ from "lodash";

export default {
  components: {
    Pagination,
    AddDialog,
    emptyData,
  },
  data() {
    return {
      addFlag: false,
      total: 0,
      pageNum: 1,
      pageSize: 20,
      pageSizes: [20, 50, 100, 300],
      tableData: [],
      typeList: [
        {
          token: "Device",
          name: "设备",
        },
      ],
      editmetaData: {},
      isedit: false,
      token: "",
    };
  },
  mounted() {
    this.init();
    // this.getAssettypes();
  },
  methods: {
    handNavTo(row) {
      const { token } = row;
      this.$router.push({
        name: "networkTypDetails",
        query: {
          assetTypeToken: token,
        },
      });
    },
    handEdit(assetToken) {
      getAssetsDetails(assetToken).then((res) => {
        const { data } = res;
        this.$nextTick(() => {
          this.$refs.addDialgRef.deviceGroupForm = data;
          this.isedit = true;
          this.editmetaData = data.metadata;
          // 此处传递的是老的token 需要深拷贝
          this.token = _.cloneDeep(data.token);
          this.$refs.addDialgRef.deviceGroupForm.assetTypeToken =
            data.assetType.token;
          // this.$refs.addDialgRef.metaDataJson = data.metadata;
        });
      });
      this.addFlag = true;
    },

    handAdd() {
      this.isedit = false;
      this.addFlag = true;
    },
    closeDialog(isRefresh) {
      // isRefresh是否刷新
      if (isRefresh) {
        this.init();
      }
      this.addFlag = false;
    },
    //分页回调
    pagination({ page, limit }) {
      this.pageNum = page;
      this.pageSize = limit;
      this.init();
    },
    // getAssettypes() {
    //   getAssettypes({ page: 1, pageSize: 0 }).then((res) => {
    //     console.log(res, "ressss");
    //     const { data } = res;
    //     this.typeList = data.results;
    //   });
    // },
    async init() {
      this.tableData = null;
      try {
        const res = await getAssettypes({
          // assetTypeToken: "123",
          includeContainedAreaTypes: false,
          page: this.pageNum,
          pageSize: this.pageSize,
        });
        const { status, data } = res;
        if (status === 200) {
          const { results, numResults } = data;
          this.total = numResults;
          this.tableData = results;
          this.$forceUpdate();
        }
      } catch (error) {
        this.tableData = [];
      }
    },
  },
};
</script>

<style lang='scss' scoped>
@import "~@/styles/mixin.scss";

.my_network_box {
  position: relative;
  padding: 20px;
  padding-bottom: 0;
  background-color: #fff;

  .item {
    display: flex;
    border: 1px solid #e4e4e4;
    border-radius: 2px;
    padding: 26px;
    &:hover {
      /* 列表选中样式 */
      cursor: pointer;
      background-color: #f3f9ff;
      border: 1px solid rgba(38, 128, 210, 1);
    }
    .content {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .title {
        color: #333;
        font-size: 16px;
        @include textoverflow(1);
      }
      .summary {
        color: #999999;
        font-size: 12px;
        @include textoverflow(2);
      }
    }

    .img {
      width: 62px;
      height: 62px;
      margin-right: 14px;
    }
  }
}
</style>
